<?php
require_once("inc.member.php");

$this_title=__("Add Stockist Sales");
$page_title=__("Add Stockist Sales");
$list_file=$vars["file"]["member"]["stockist_plan_sales"];

if(!$get_s["uid"]){
  $errmsg=__("No stockist with that ID found!");
}elseif(!@mysql_num_rows($r=mysql_query("select * from $db->users where id='$get_d[uid]'"))){
  $errmsg=__("No stockist with that ID found!");
}else{
  $r_user=mysql_fetch_assoc($r);
}

if($errmsg){
 $errmsg="<h2>$page_title</h2>".format_err($errmsg);

 print format_member_page($errmsg, $this_title, $content_title);
 exit();
}

$td_width=120;
$datetime=ndate($vars["system_date_format"]);

//#####ADD STOCKIST SALES POST#####
if($_POST["__req"]){
 //#####ERROR CHECK#####
 $chk_arr="bv";
 $chk_arrr=explode(",", $chk_arr);
 foreach($post_s as $f=>$v){
  if(in_array($f, $chk_arrr)){
   $data[$f]=$v;
  }
 }
 $errmsg=verify_form_data("add_stockist_sales", $data);
 //#####END ERROR CHECK#####

 //#####ADD TO DB#####
 if(!$errmsg){  
   if(is_numeric($post_d[bv])){
	    $sql="insert into $db->add_stockist_sales (uid,stockist_uid,stockist_assign_date,bv,cdate) values (".$r_user["id"].",".$r_user["stockist_uid"].",'".$r_user["stockist_assign_date"]."','$post_d[bv]','$datetime')";
		
		if(!mysql_query($sql)){
			$errmsg.="Error adding stockist sales.<br />\n<br />\nSQL: $sql<br />\n<br />\nError: ".mysql_error()."<br />\n";
		}else{
			$new_cid=mysql_insert_id();
		}
		if(!$errmsg){
			$msg=replace_tag(__("Total <%bv%>BV sales has been added."), array("<%bv%>"=>$post_d[bv]))."<br />\n";
		}
   }else{
		$msg=__("Invalid input.")."<br />\n";
   }
 }

 $msg=$msg? format_msg($msg) : "";
 $errmsg=$errmsg? format_err(__("There is some error(s), please correct them before continuing:")."<br />\n<br />\n$errmsg") : "";
}
//#####END STOCKIST SALES POST#####

$form_fields=array("id"=>"","stockist_code"=>"","bv"=>"");
foreach($form_fields as $field => $default){
 $db_fieldname=preg_match('/^_/', $field)? substr($field, 1) : $field;
 $dis[$field]=(!$post_s["__req"]? ($_GET["type"]=="edit"? nhtmlentities($r_user[$db_fieldname]) : $default) : $post_h[$field]);
}

//javascript
$jvscript=
"<script type='text/javascript' src='".JS_URL."/get_file_gzip.php?file=".urlencode("common.js,jquery.js")."'></script>
<script type='text/javascript'>
jQuery(document).ready(function(j){
 j('form[@name=user_form]').submit(function(){
  j('input[@name=submit_btn]').attr('disabled','disabled');
 });
});
</script>";

//back button
$back_url=$list_file;

//format input row
$display_fields=array("bv");

foreach($form_fields as $field => $default){
 if(in_array($field, $display_fields)){
  $db_fieldname=preg_match('/^_/', $field)? substr($field, 1) : $field;
  $dbr=explode("#", $vars["dbr"]["add_stockist_sales"][$db_fieldname]);
  $readonly=$textarea=false;
  $extra_display='';
  if($field=='stockist_code'){   
   $readonly=true;
  }else{
   $extra_display='';
  }
  $readonly_str=$readonly? "readonly='readonly'" : "";
  $form_inputfield[$db_fieldname]="
  <tr>
   <td width='$td_width'>".__($dbr[4]).__(":").($dbr[3]=='m'? " ".__("*") : "")."</td>
   <td>".($textarea? "<textarea name='$field' rows='6' $inputbox_style $readonly_str>".$dis[$field]."</textarea>" : "
   <input type='text' name='$field' $inputbox_style value=\"".$dis[$field]."\" $readonly_str />")."$extra_display</td>
  </tr>";
 }
}

$manage_user=($errmsg || $msg? $errmsg.$msg : "").
"<form name='aj_form' method='post' action='$this_file'></form>
<form name='user_form' method='post' action='$this_file?$_SERVER[QUERY_STRING]'>
<input type='hidden' name='__req' value='1' />
<input type='hidden' name='back_url' value=\"$back_url\" />
<table class='amt_table' width='60%'>
 <tr class='amt_header'>
  <td colspan='2'></td>
 </tr>
 <tr>
  <td width='$td_width'>".__("Stockist ID").__(":")."</td>
  <td>$get_s[uid]</td>
 </tr>
 $form_inputfield[bv]
 <tr>
  <td colspan='2' class='center' style='padding:20px 0 20px 0;'>
   <input type='button' value=\"".__("Back")."\" onclick=\"location='$back_url';\">&nbsp;&nbsp;&nbsp;
   <input type='submit' name='submit_btn' value=\"".($_GET["type"]=="edit"? __("Update Sales") : __("Add Sales"))."\" />
  </td>
</table>
</form>
$c_jvscript";

$content="<h2>$page_title</h2>$manage_user";

print format_member_page($content, $this_title, $content_title, $css.$jvscript);
?>